﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApiTest.Dto.System;
using WebApiTest.Dto.Utils;
using WebApiTest.Service.System;

namespace WebApiTest.Controllers.system
{
    [Route("api/[controller]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private RoleService roleService = new RoleService();

        [HttpPost("query")]
        public TableRspDTO list4Table(TableReqDTO tableReqDTO)
        {
            TableRspDTO tableRspDTO = new TableRspDTO();
            try
            {
                tableRspDTO = roleService.list4Table(tableReqDTO);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return tableRspDTO;
        }

        [HttpPost("add")]
        public OpResultDTO add(RoleDTO roleDTO)
        {
            OpResultDTO opResult = new OpResultDTO();
            try
            {
                opResult.message = roleService.add(roleDTO).ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                opResult.message = "error";
            }
            return opResult;
        }

        [HttpPost("update")]
        public OpResultDTO update(RoleDTO roleDTO)
        {
            OpResultDTO opResult = new OpResultDTO();
            try
            {
                opResult.message = roleService.update(roleDTO).ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                opResult.message = "error";
            }
            return opResult;
        }

        [HttpPost("remove")]
        public OpResultDTO remove(JObject request)
        {
            OpResultDTO opResult = new OpResultDTO();
            try
            {
                opResult.message = roleService.remove(request["roleIds"].ToString()).ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                opResult.message = "error";
            }
            return opResult;
        }

        [HttpPost("listPermissionByRoleId4Tree")]
        public OpResultDTO listPermissionByRoleId4Tree(JObject request)
        {
            OpResultDTO opResult = new OpResultDTO();
            try
            {
                opResult.result = roleService.listPermissionByRoleId4Tree(long.Parse(request["roleId"].ToString()));
                opResult.message = "success";
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                opResult.message = "error";
            }
            return opResult;
        }

        [HttpPost("listPermission4Tree")]
        public OpResultDTO listPermission4Tree()
        {
            OpResultDTO opResult = new OpResultDTO();
            try
            {
                opResult.result = roleService.listPermission4Tree();
                opResult.message = "success";
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                opResult.message = "error";
            }
            return opResult;
        }

        [HttpPost("authorize")]
        public OpResultDTO authorize(JObject request)
        {
            OpResultDTO opResult = new OpResultDTO();
            try
            {
                opResult.message = roleService.authorize(request).ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                opResult.message = "error";
            }
            return opResult;
        }

        [HttpPost("listRole")]
        public OpResultDTO listRole()
        {
            OpResultDTO opResult = new OpResultDTO();
            try
            {
                opResult.result = roleService.listRole();
                opResult.message = "success";
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                opResult.message = "error";
            }
            return opResult;
        }
    }
}
